require(['init'], function (init) {
  if(!localStorage.lang){
    localStorage.setItem('lang','zh');
  }
  if(localStorage.lang && localStorage.lang=='zh'){
    $('body').append('<script src="codebase/locale/locale_cn.js"  type="text/javascript" charset="utf-8"></script>');
    $('body').append('<script src="codebase/locale/recurring/locale_recurring_cn.js" charset="utf-8"></script>');
  }
  else {
    $('body').append('<script src="codebase/locale/locale_en.js"  type="text/javascript" charset="utf-8"></script>');
    $('body').append('<script src="codebase/locale/recurring/locale_recurring_en.js" charset="utf-8"></script>');
  }
  var DEFAULT_CLASSTYPEID = '6'

  var tuid;
  if(isIframe()) {
    var curIframeUrl = parent.document.getElementById("content_iframe").src
    tuid = curIframeUrl.urlToObj().tuid;
  } else {
    tuid = location.search.urlToObj().tuid
  }


  function loadTeacherData(teacheruid, callback) {
    if(teacheruid == undefined) {
      return;
    }
    ajaxMethod({
      url: '/api/teacher/' + teacheruid,
      success: function (data) {
        $('#J_name').text(data.lastname);
        $('#J_phone').text(data.mobile);
        $('[name=tuid]').val(data.teacheruid);
        $('#J_remark1').text(data.remark1 || '');
        $('#J_remark2').text(data.remark2 || '');

        (typeof callback == 'function') && callback()
      }
    })
  }

  $.when(
    $.ajax('/api/coursetypes'),
    $.ajax('/api/eeo/classtypes')
  ).done(function () {
    var resArr = [].slice.call(arguments,0,4)
    var coursetypes = resArr[0][0].data
    var classtypes = resArr[1][0].data

    console.log(coursetypes);
    console.log(classtypes);
    coursetypes = [
      {
        key: 0,
        label:i18next.t('table.pleasecourse')//请选择课程类型
      }
    ].concat($.map(coursetypes, function (item) {
      return {
        key: item.coursetypeuid,
        label: item.coursetypename,
        color: item.colorvalue
      }
    }))

    classtypes = $.map(classtypes, function (item) {
      return {
        key: item.id,
        label: item.name
      }
    })

    var create_lightbox = [
      {
        name: i18next.t('table.totalquota'),//总名额
        type: "textarea",
        map_to: "quotas",
        height:30,
        default_value: 1
      },
      {
        name:i18next.t('course.coursetype'),//课程类型
        type: "select",
        map_to: "coursetypeid",
        options: coursetypes,
        default_value: '0',
        height:30
      },
      {
        name:i18next.t('table.attendance'), //"上台人数",
        type: "select",
        map_to: "classtypeid",
        options: classtypes,
        default_value: DEFAULT_CLASSTYPEID,
        height:30
      },
      {
        name: i18next.t('table.lessonsname'),//课节名称
        type: "textarea",
        map_to: "lessonname",
        height: 30,
        default_value: ''
      },
      {
        name:  i18next.t('table.classresources'),//课节资源
        type: "my_editor",
        map_to: "resource",
        default_value:i18next.t('table.pleaseclassresources') ,//'请选择课节资源',
        height: 35
      },
      {
        name: i18next.t('table.auditor'),//旁听生
        type: "audit",
        default_value: i18next.t('table.pleaseauditor'),//'请选择旁听生',
        height: 35
      },
      {
        name: i18next.t('popupbox.courselevel'),//"课程级别",
        type: "level",
        default_value:  i18next.t('popupbox.addcourselevel'),//'请添加课程级别',
        height: 35
      },
      {
        name: i18next.t('table.webcaststatus'),//"直播状态",
        type: "webcast",
        height:30
      },
      {
        name:"recurring",
        type:"recurring",
        map_to:"rec_type",
        button:"recurring",
        height:30
        // form:"my_recurring_form"
      },
      {
        name: "time",
        type: "time",
        map_to: "auto",
        height: 30
      }
    ]

    var edit_lightbox = [
      {
        name: i18next.t('table.totalquota'),//总名额
        type: "textarea",
        map_to: "quotas",
        height: 30
      },
      {
        name: i18next.t('table.quotaused'),//已用名额
        type: "template",
        map_to: "usequotas",
        height: 30
      },
      {
        name:i18next.t('course.coursetype'),//课程类型
        type: "select",
        map_to: "coursetypeid",
        options: coursetypes,
        default_value: '0',
        height:30
      },
      {
        name:i18next.t('table.attendance'), //"上台人数",
        type: "select",
        map_to: "classtypeid",
        options: classtypes,
        default_value: DEFAULT_CLASSTYPEID,
        height:30
      },
      {
        name: i18next.t('table.lessonsname'),//课节名称
        type: "textarea",
        map_to: "lessonname",
        height: 30,
        default_value: ''
      },
      {
        name:  i18next.t('table.classresources'),//课节资源
        type: "my_editor",
        map_to: "resource",
        default_value:i18next.t('table.pleaseclassresources') ,//'请选择课节资源',
        height: 35
      },
      {
        name:i18next.t('info.studentlist'),//学生列表
        type: "template",
        map_to: "students",
        height: 30
      },
      {
        name: i18next.t('table.auditor'),//旁听生
        type: "audit",
        // map_to: "resource",
        default_value: i18next.t('table.pleaseauditor'),//'请选择旁听生',
        height: 35
      },
      {
        name: i18next.t('popupbox.courselevel'),//"课程级别",
        type: "level",
        default_value:  i18next.t('popupbox.addcourselevel'),//'请添加课程级别',
        height: 35
      },
      {
        name: i18next.t('table.webcaststatus'),//"直播状态",
        type: "webcast"
      },
      {
      	name: i18next.t('table.mvurl'), //'视频地址',
        type: 'videosite'
      },
      {
        name: "time",
        type: "time",
        map_to: "auto",
        height: 30
      }
    ]

    var read_lightbox = [
      {
        name: i18next.t('table.totalquota'),//总名额
        type: "template",
        map_to: "quotas",
        height: 30
      },
      {
        name: i18next.t('table.quotaused'),//已用名额
        type: "template",
        map_to: "usequotas",
        height: 30
      },
      {
        name:i18next.t('course.coursetype'),//课程类型
        type: "template",
        map_to: "coursetypename",
        // options: courseList,
        default_value: '0',
        height: 30
      },
      {
        name: i18next.t('table.lessonsname'),//课节名称
        type: "template",
        map_to: "lessonname",
        height: 30,
        default_value: ''
      },
      {
        name:  i18next.t('table.classresources'),//课节资源
        type: "template",
        map_to: "foldername"
      },
      {
        name:i18next.t('info.studentlist'),//学生列表
        type: "template",
        map_to: "students",
        height: 30
      },
      {
        name: i18next.t('table.auditor'),//旁听生
        type: "template",
        map_to: "auditors"
      },
      {
        name: "time",
        type: "time",
        map_to: "auto",
        height: 30
      }
    ]

    schedulerInit({
      tuid: tuid,
      helpUrl: '/api/teacherworktimes',
      url: '/api/schedule',
      checkUrl: '/api/schedules/check',
      courseList: coursetypes,
      classtypes: classtypes,
      schedulerType: 2,
      create_lightbox: create_lightbox,
      edit_lightbox: edit_lightbox,
      read_lightbox: read_lightbox,
      onCreate: true,
      processData: function(data, courseList) {
        var dataArr = []
        $.each(data, function (i,v) {

          //学生
          var studentArr = v.students || [];
          var studentNameArr = [];
          $.each(studentArr, function () {
            studentNameArr.push(this.name);
          });

          //旁听
          var auditArr = v.auditors || [];
          var auditNameArr = [];
          $.each(auditArr, function () {
            auditNameArr.push(this.name);
          });

          var cur = {
            start_date: new Date(v.begintime),
            end_date: new Date(v.endtime),
            timeuid: v.scheduleid,
            // text: v.coursetypename,
            text: v.name,

            quotas: v.quotas || '0',
            usequotas: v.usequotas || '0',
            coursetypeid: v.coursetypeid || '0',
            classtypeid: v.classtypeid || DEFAULT_CLASSTYPEID,
            coursetypename: v.coursetypename || '',
            students: studentNameArr.join(', ') ||i18next.t('table.empty'),
            color: getCourseColorById(courseList, v.coursetypeid) || '#96d6e3',
            edit: true,
            lessonname: v.name || '',
            folderid: v.folderid,
            foldername: v.foldername,
            auditors: v.auditors,
            type: v.type, //type 1:约课 2:班课
            castinfo: v.castinfo,
            webcast: v.webcast, //直播链接
            levelid: v.levelid, //
	          videosite: v.videosite
          }

          if(v.type == 2) {
            cur.auditors = auditNameArr.join(', ') ||i18next.t('table.empty')
          }

          if(cur.quotas != '0' && cur.quotas === cur.usequotas) {
            cur.color = 'red';
          }
          dataArr.push(cur)
        })
        return dataArr;
      }
    })

  })

  loadTeacherData(tuid)

})
